home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: how can an int data type accept char data?
- Date: 5 Mar 1996 07:41:58 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4hhnc6INNqlk@keats.ugrad.cs.ubc.ca>
- References: <4hg2si$irt@news.azstarnet.com>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4hg2si$irt@news.azstarnet.com>,
- Howard Salmon <captarm@azstarnet.com> wrote:
- >I thought that the int data type could only accept integer data;
- >however, the example listed below (taken from Dave Mark's book "Learn C
- >on the Macintosh") shows an int variable ("done") accepting character
- >data (i.e. "FALSE"). Dave Mark doesnt explain this. Can anyone help me
- >out? (This is a program is supposed to find the prime number that
- >follows a previous prime number):
-
- FALSE is probably a symbolic constant implemented via a pre-processor macro. It
- is not declared anywhere, and <stdio.h> does not define such a constant.
-
- Are you sure you typed this program in 100% verbatim?
-
-
- In any case, integers in C _can_ hold character constants. A character is what
- is called an _integral type_. In expressions, small integral types such as bit
- fields and characters are automatically converted to type int, if that can be
- used to represent all their values, otherwise to unsigned int. Thus, an
- expression like this is valid:
-
- int x;
-
- ...
-
- x += 'A';
-
- This will add the collating value of the character a to the integer variable x.
- (For example, 65 if your compiler's translation character set is ascii).
-
- >#include <stdio.h>
- >
- >main()
- >{
- > int startingPoint, candidate, i;
- > int done, foundFactor;
- >
- > done = FALSE;
-
- This will yield an error, since FALSE is not declared. C allows implicit
- declarations only for funtions. YOu can call a function for example ``foo();''
- that you have not declared, and an implicit declaration will be generated by
- the compiler, which looks like ``int foo()''. If you call a function that
- doesn't exist anywhere in your program, or libraries and modules linked to it,
- the error will typically only be caught at link time, whereby the linker will
- typically complain of unresolved references (assuming you are programming C in
- an enviroment where the concept of linker is meaningful).
-
- However, if you reference any other kind of object that is not declared, you
- will get an error. In the above, the compiler will complain that FALSE has not
- been declared, that its type is unknown, etc.
-
- >
- > {how can a variable declared as an integer
- > hold character variables? Why wouldn't
- > C insist on a char data type? }
-
- A character string requires quotes. If you wanted a character string FALSE, you
- would have to write it "FALSE". The type of such a string literal is ``an array
- of (length + 1) char'', and it evaluates to a pointer to the first element.
- To create one and assign the address of its first element to a pointer, you can
- do this:
-
- char *mystring = "FALSE";
-
- The type of "FALSE" is truly incompatible with integers, because it is a
- pointer to a character. On the other hand, the expression mystring[3] is an
- integral type, the character 'S'.
- --
-
-